package org.jsola.hr.helper;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.jsola.common.SafeKit;
import org.jsola.hr.dto.AttendPeopleMonthlyStatAddDTO;
import org.jsola.hr.dto.AttendPunchDTO;
import org.jsola.hr.dto.attendrule.*;
import org.jsola.hr.dto.attendschedule.AttendPeriodDTO;
import org.jsola.hr.entity.AttendPunchRecordDO;
import org.jsola.hr.entity.EmpInfoDO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.query.AttendPunchCheckQuery;
import org.jsola.hr.service.*;
import org.jsola.hr.vo.*;
import org.jsola.module.ModuleContext;
import org.jsola.user.core.TokenUser;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static org.jsola.hr.core.TestCommonConstans.*;

@Component
@Slf4j
public class AttendRulePlanTestHelper extends BaseTestHelper {

    @Autowired
    private AttendPeriodTestHelper attendPeriodTestHelper;
    @Autowired
    private IAttendRuleService attendRuleService;
    @Autowired
    private EmpDepTestHelper empDepTestHelper;
    @Autowired
    private AttendRulePlanTestHelper attendRulePlanTestHelper;
    @Autowired
    private IAttendPunchService attendPunchService;
    @Autowired
    private IAttendPunchRecordService attendPunchRecordService;
    @Autowired
    private IEmpInfoService empInfoService;
    @Autowired
    private IAttendPeopleMonthlyStatService attendPeopleMonthlyStatService;


    /**
     * 考勤打卡
     *
     */
    @Test
    public void attendRulePlan(ModuleContext moduleContext) {

        List<AttendPunchRecordDO> attendPunchRecordList = new ArrayList<AttendPunchRecordDO>();

        List<AttendPeopleMonthlyStatVO> attendPeopleMonthlyStatList = new ArrayList<>();

        // 构建一条待添加的考勤规则
        AttendRuleAddDTO attendRuleAddDTO = attendRulePlanTestHelper.getFixedAttendRuleAddDTO(moduleContext);
        // 保存考勤方案
        attendRuleService.save(attendRuleAddDTO, buildTokenUser());

        log.info("保存考勤方案....");

        // 考勤打卡
        Map<String, List<EmpInfoVO>> depUserMap = moduleContext.get(CODE_DEP_EMP_MAP);
        for (Map.Entry<String, List<EmpInfoVO>> entry : depUserMap.entrySet()) {

            List<EmpInfoVO> empInfoList = entry.getValue();
            empInfoList.forEach(empInfo -> {

                try {

                    // 打卡人员月统计
                    AttendPeopleMonthlyStatAddDTO monthlyStat = new AttendPeopleMonthlyStatAddDTO();
                    // 公司ID
                    monthlyStat.setCompanyId(empInfo.getCompanyId());
                    // 公司名称
                    monthlyStat.setCompanyName("");
                    // 部门ID
                    monthlyStat.setDeptId("");
                    // 部门名称
                    monthlyStat.setDeptName("");
                    // 员工ID
                    monthlyStat.setEmpInfoId(empInfo.getId());
                    // 员工姓名
                    monthlyStat.setEmpInfoName(empInfo.getName());
                    // 工号
                    monthlyStat.setJobNumber("");
                    // 年
                    monthlyStat.setYearly(DateFormatUtils.format(new Date(), "yyyy"));
                    // 月
                    monthlyStat.setMonthly(DateFormatUtils.format(new Date(), "MM"));
                    // 应出勤天数
                    monthlyStat.setShouldWorkDays(Integer.valueOf(getRandomValue("int",0)).intValue());
                    // 出勤天数
                    monthlyStat.setAttendDays(Integer.valueOf(getRandomValue("int",0)).intValue());
                    // 休息天数
                    monthlyStat.setRestDays(Integer.valueOf(getRandomValue("int",0)).intValue());
                    // 迟到次数
                    monthlyStat.setLateCounts(Integer.valueOf(getRandomValue("int",0)).intValue());
                    // 上班缺卡次数
                    monthlyStat.setOnWorkMissCounts(Integer.valueOf(getRandomValue("int",0)).intValue());
                    // 下班缺卡次数
                    monthlyStat.setOffWorkMissCounts(Integer.valueOf(getRandomValue("int",0)).intValue());
                    // 旷工天数
                    monthlyStat.setAbsenteeismDays(new BigDecimal(getRandomValue("int",0)));
                    // 请假天数
                    monthlyStat.setLeaveDays(new BigDecimal(getRandomValue("int",0)));
                    if (monthlyStat.getShouldWorkDays() == 0) {
                        monthlyStat.setShouldWorkDays(20);
                    }

                    AttendPeopleMonthlyStatVO attendPeopleMonthlyStatVO = attendPeopleMonthlyStatService.save(monthlyStat, buildEmpInfoTokenUser(empInfo));

                    attendPeopleMonthlyStatList.add(attendPeopleMonthlyStatVO);

                    log.info("考勤打卡人员---> {}", empInfo.getName());

                    // 检查考勤打卡范围
                    AttendPunchCheckQuery attendPunchCheckQuery = new AttendPunchCheckQuery();
                    attendPunchCheckQuery.setLatitude("40.010055");
                    attendPunchCheckQuery.setLongitude("116.644009");
                    attendPunchCheckQuery.setWifiName("");
                    attendPunchCheckQuery.setMacAddr("");
                    attendPunchCheckQuery.setPunchLocation("中央人民广场");
                    AttendPunchCheckVO attendPunchCheckVO = attendPunchService.checkPunch2(attendPunchCheckQuery, buildEmpInfoTokenUser(empInfo));

                    log.info("检查考勤打卡范围结果:{}" + JSON.toJSONString(attendPunchCheckVO));

                    // 今日是否需要考勤;true-是,false-否
                    if (!attendPunchCheckVO.getIsTodayAttend()) {
                        log.info("今日不需要考勤....");
                        return;
                    }
                    // 是否白名单;true-是,false-否
                    if (attendPunchCheckVO.getIsWhitelist()) {
                        log.info("白名单不需要考勤....");
                        return;
                    }
                    // 特殊日期是否需要打卡;true-是,false-否
                    if (attendPunchCheckVO.getIsSpecialDate() && !attendPunchCheckVO.getIsNeedPunchOfDaily()) {
                        log.info("特殊日期不需要考勤....");
                        return;
                    }
                    // 是否在打卡范围内;1-是,0-否
                    if (attendPunchCheckVO.getRangeState() == 0) {
                        log.info("不在打卡范围内....");
                        return;
                    }

                    AttendPunchDTO attendPunchDTO = new AttendPunchDTO();
                    // 经度
                    attendPunchDTO.setLongitude("116.644009");
                    // 纬度
                    attendPunchDTO.setLatitude("40.010055");
                    attendPunchDTO.setWifiName("");
                    attendPunchDTO.setMacAddr("");
                    // 打卡序号
                    attendPunchDTO.setPunchNumber(attendPunchCheckVO.getPunchNumber());

                    AttendPunchVO attendPunchVO = attendPunchService.punch2(attendPunchDTO, buildEmpInfoTokenUser(empInfo));

                    log.info("打卡结果：{}" + JSON.toJSONString(attendPunchVO));

                    // 根据考勤记录ID查询
                    AttendPunchRecordDO attendPunchRecordDO = attendPunchRecordService.selectDOById(attendPunchVO.getPunchRecordId(), empInfo.getSiteId());
                    if (attendPunchRecordDO != null) {
                        attendPunchRecordList.add(attendPunchRecordDO);
                    }

                }catch (HrException e) {
                    // log.info("跳过异常...{}", e.getMessage());
                }

            });
        }

        if (attendPunchRecordList != null && attendPunchRecordList.size() > 0) {
            attendPunchRecordList.forEach(attendPunchRecord -> {

                EmpInfoDO empInfoDO = empInfoService.selectDOById(attendPunchRecord.getEmpInfoId(), attendPunchRecord.getSiteId());
                if (empInfoDO != null) {
                    log.info("员工信息:" + JSON.toJSONString(empInfoDO));
                }

                log.info("考勤记录:" + JSON.toJSONString(attendPunchRecord));
            });
        }

        // 将考勤记录添加到上下文
        moduleContext.add(ATTEND_RECORD_LIST, attendPunchRecordList);
        // 打卡人员月统计
        moduleContext.add(ATTEND_PEOPLE_MONTHLY_STAT, attendPeopleMonthlyStatList);
    }

    /**
     * 获取考勤打卡规则
     *
     * @param moduleContext
     * @return
     */
    public AttendRuleAddDTO getFixedAttendRuleAddDTO(ModuleContext moduleContext) {

        List<DepInfoVO> depList = moduleContext.get(CODE_DEP_LIST);
        CompanyVO companyVO = moduleContext.get(CODE_COMPANY);
        List<EmpInfoVO> empInfoList = moduleContext.get(CODE_EMP_LIST);

        AttendRuleAddDTO attendRuleAddDTO = new AttendRuleAddDTO();

        // 方案名称
        String planName = "测试-考勤规则";
        // 方案类型 ; 1-固定时间上下班,2-按班次上下班,3-自由上下班
        int planType = 1;

        attendRuleAddDTO.setCompanyId(companyVO.getId());
        // 打卡部门
        List<AttendRuleDeptAddDTO> deptList = this.getDeptList(depList);
        // 打卡人员
        List<AttendRuleStaffAddDTO> staffList = null;// this.getStaffList(empInfoList, depList);
        // 白名单
        List<AttendRuleWhitelistAddDTO> whitelist = null; // this.getWhitelist(empInfoList, depList);
        // 汇报对象
        List<AttendRuleReportAddDTO> reportList = this.getReportList(empInfoList, depList);
        // 打卡地点-位置
        List<AttendRuleLocationAddDTO> locationList = this.getLocationList();
        // 打卡地点-wifi
        List<AttendRuleWifiAddDTO> wifiList = this.getWifiList();
        // 打卡时间
        List<AttendRulePunchTimeAddDTO> punchTimeList = this.getPunchTimeListRandom();
        // 特殊日期-必须打卡的日期
        List<NeedPunchDateAddDTO> needPunchDateList = this.getNeedPunchDateList();
        // 特殊日期-不必打卡的日期
        List<NoNeedPunchDateAddDTO> noNeedPunchDateList = this.getNoNeedPunchDateList();
        // 加班规则 1-以加班申请为准,2-以打卡时间为准,3-以加班申请核算打卡记录
        int overtimeRule = 1;
        // 是否同步中国节假日不能为空
        Boolean isSynchLegalHoliday = false;
        // 是否手机提醒;1-是,0-否
        Boolean isRemind = false;
        // 上班前x分钟
        int remindBefore = 0;
        // 下班后x分钟
        int remindAfter = 0;
        // 员工打卡时必须拍照
        Boolean isTakePhoto = false;
        // 只能拍照
        Boolean isOnlyPhoto = false;
        // 范围外打卡
        int punchRange = 0;
        // 是否补卡申请
        Boolean isMakeUp = false;
        // 每月不高于x下次
        int maxMakeUpCounts = 0;
        // 生效时间
        int takeEffect = 0;

        // 方案名称
        attendRuleAddDTO.setPlanName(planName);
        // 规则类型不能为空 (1-固定时间上下班,2-按班次上下班,3-自由上下班)
        attendRuleAddDTO.setPlanType(planType);
        // 打卡部门
        attendRuleAddDTO.setDeptList(deptList);
        // 打卡人员
        attendRuleAddDTO.setStaffList(staffList);
        // 白名单
        attendRuleAddDTO.setWhitelist(whitelist);
        // 汇报对象
        attendRuleAddDTO.setReportList(reportList);
        // 打卡地点-位置
        attendRuleAddDTO.setLocationList(locationList);
        // 打卡地点-wifi
        attendRuleAddDTO.setWifiList(wifiList);
        // 打卡时间
        attendRuleAddDTO.setPunchTimeList(punchTimeList);
        // 特殊日期-必须打卡的日期
        attendRuleAddDTO.setNeedPunchDateList(needPunchDateList);
        // 特殊日期-不必打卡的日期
        attendRuleAddDTO.setNoNeedPunchDateList(noNeedPunchDateList);
        // 加班规则不能为空
        attendRuleAddDTO.setOvertimeRule(overtimeRule);
        // 是否同步中国节假日不能为空
        attendRuleAddDTO.setIsSynchLegalHoliday(isSynchLegalHoliday);
        // 是否手机提醒
        attendRuleAddDTO.setIsRemind(isRemind);
        // 上班前x分钟
        attendRuleAddDTO.setRemindBefore(remindBefore);
        // 下班后x分钟
        attendRuleAddDTO.setRemindAfter(remindAfter);
        // 员工打卡时必须拍照不能为空
        attendRuleAddDTO.setIsTakePhoto(isTakePhoto);
        // 只能拍照
        attendRuleAddDTO.setIsOnlyPhoto(isOnlyPhoto);
        // 范围外打卡
        attendRuleAddDTO.setPunchRange(punchRange);
        // 是否补卡申请
        attendRuleAddDTO.setIsMakeUp(isMakeUp);
        // 每月不高于x下次
        attendRuleAddDTO.setMaxMakeUpCounts(maxMakeUpCounts);
        // 生效时间
        attendRuleAddDTO.setTakeEffect(takeEffect);

        return attendRuleAddDTO;
    }

    public AttendPlanVO saveAttendRule() {

        AttendRuleAddDTO attendRuleAddDTO = new AttendRuleAddDTO();

        // 方案名称
        String planName = "工作日考勤规则一";
        // 方案类型 ; 1-固定时间上下班,2-按班次上下班,3-自由上下班
        int planType = 1;
        // 打卡部门
        List<AttendRuleDeptAddDTO> deptList = this.getDeptList();
        // 打卡人员
        List<AttendRuleStaffAddDTO> staffList = this.getStaffList();
        // 白名单
        List<AttendRuleWhitelistAddDTO> whitelist = this.getWhitelist();
        // 汇报对象
        List<AttendRuleReportAddDTO> reportList = this.getReportList();
        // 打卡地点-位置
        List<AttendRuleLocationAddDTO> locationList = this.getLocationList();
        // 打卡地点-wifi
        List<AttendRuleWifiAddDTO> wifiList = this.getWifiList();
        // 打卡时间
        List<AttendRulePunchTimeAddDTO> punchTimeList = this.getPunchTimeList();
        // 特殊日期-必须打卡的日期
        List<NeedPunchDateAddDTO> needPunchDateList = this.getNeedPunchDateList();
        // 特殊日期-不必打卡的日期
        List<NoNeedPunchDateAddDTO> noNeedPunchDateList = this.getNoNeedPunchDateList();
        // 加班规则 1-以加班申请为准,2-以打卡时间为准,3-以加班申请核算打卡记录
        int overtimeRule = 1;
        // 是否同步中国节假日不能为空
        Boolean isSynchLegalHoliday = false;
        // 是否手机提醒;1-是,0-否
        Boolean isRemind = false;
        // 上班前x分钟
        int remindBefore = 0;
        // 下班后x分钟
        int remindAfter = 0;
        // 员工打卡时必须拍照
        Boolean isTakePhoto = false;
        // 只能拍照
        Boolean isOnlyPhoto = false;
        // 范围外打卡
        int punchRange = 1;
        // 是否补卡申请
        Boolean isMakeUp = false;
        // 每月不高于x下次
        int maxMakeUpCounts = 0;
        // 生效时间
        int takeEffect = 0;

        // 方案名称
        attendRuleAddDTO.setPlanName(planName);
        // 规则类型不能为空 (1-固定时间上下班,2-按班次上下班,3-自由上下班)
        attendRuleAddDTO.setPlanType(planType);
        // 打卡部门
        attendRuleAddDTO.setDeptList(deptList);
        // 打卡人员
        attendRuleAddDTO.setStaffList(staffList);
        // 白名单
        attendRuleAddDTO.setWhitelist(whitelist);
        // 汇报对象
        attendRuleAddDTO.setReportList(reportList);
        // 打卡地点-位置
        attendRuleAddDTO.setLocationList(locationList);
        // 打卡地点-wifi
        attendRuleAddDTO.setWifiList(wifiList);
        // 打卡时间
        attendRuleAddDTO.setPunchTimeList(punchTimeList);
        // 特殊日期-必须打卡的日期
        attendRuleAddDTO.setNeedPunchDateList(needPunchDateList);
        // 特殊日期-不必打卡的日期
        attendRuleAddDTO.setNoNeedPunchDateList(noNeedPunchDateList);
        // 加班规则不能为空
        attendRuleAddDTO.setOvertimeRule(overtimeRule);
        // 是否同步中国节假日不能为空
        attendRuleAddDTO.setIsSynchLegalHoliday(isSynchLegalHoliday);
        // 是否手机提醒
        attendRuleAddDTO.setIsRemind(isRemind);
        // 上班前x分钟
        attendRuleAddDTO.setRemindBefore(remindBefore);
        // 下班后x分钟
        attendRuleAddDTO.setRemindAfter(remindAfter);
        // 员工打卡时必须拍照不能为空
        attendRuleAddDTO.setIsTakePhoto(isTakePhoto);
        // 只能拍照
        attendRuleAddDTO.setIsOnlyPhoto(isOnlyPhoto);
        // 范围外打卡
        attendRuleAddDTO.setPunchRange(punchRange);
        // 是否补卡申请
        attendRuleAddDTO.setIsMakeUp(isMakeUp);
        // 每月不高于x下次
        attendRuleAddDTO.setMaxMakeUpCounts(maxMakeUpCounts);
        // 生效时间
        attendRuleAddDTO.setTakeEffect(takeEffect);

        return attendRuleService.save(attendRuleAddDTO, buildTokenUser());
    }


    /**
     * 打卡部门
     *
     * @return
     */
    public List<AttendRuleDeptAddDTO> getDeptList() {

        List<AttendRuleDeptAddDTO> deptList = new ArrayList<>();

        // 部门一
        AttendRuleDeptAddDTO dept1 = new AttendRuleDeptAddDTO();
        dept1.setDeptId("42DA81D823034735AE8F0C7E4A2ECE02");
        dept1.setDeptName("部门一");
        deptList.add(dept1);

        // 部门二
        AttendRuleDeptAddDTO dept2 = new AttendRuleDeptAddDTO();
        dept1.setDeptId("4C691F73374343AA9337082F22138038");
        dept1.setDeptName("部门二");
        deptList.add(dept2);

        return deptList;
    }

    /**
     * 随机获取部门
     *
     * @param depList
     * @return
     */
    public List<AttendRuleDeptAddDTO> getDeptList(List<DepInfoVO> depList) {

        List<AttendRuleDeptAddDTO> deptList = new ArrayList<>();

        if (depList != null && depList.size() > 0) {

            for (DepInfoVO depInfoVO : depList) {
                AttendRuleDeptAddDTO dept = new AttendRuleDeptAddDTO();
                dept.setDeptId(depInfoVO.getId());
                dept.setDeptName(depInfoVO.getName());
                deptList.add(dept);
            }
        }

        return deptList;
    }


    public List<AttendRuleStaffAddDTO> getStaffList(List<EmpInfoVO> empInfoList, List<DepInfoVO> depList) {

        List<AttendRuleStaffAddDTO> staffList = new ArrayList<>();

        if (empInfoList != null && empInfoList.size() > 0) {

            empInfoList.forEach(empInfo -> {

                AttendRuleStaffAddDTO staff1 = new AttendRuleStaffAddDTO();
                staff1.setEmpInfoId(empInfo.getId());
                staff1.setEmpInfoName(empInfo.getName());
                staff1.setDepInfoName(empInfo.getName());
                staff1.setDepInfoId(depList.get(0).getId());
                staffList.add(staff1);

            });
        }

        return staffList;
    }

    /**
     * 打卡人员
     *
     * @return
     */
    public List<AttendRuleStaffAddDTO> getStaffList() {

        List<AttendRuleStaffAddDTO> staffList = new ArrayList<>();

        AttendRuleStaffAddDTO staff1 = new AttendRuleStaffAddDTO();
        staff1.setEmpInfoId("1940931DC4564ECDA5373D212429452A");
        staff1.setEmpInfoName("员工一");
        staffList.add(staff1);

        AttendRuleStaffAddDTO staff2 = new AttendRuleStaffAddDTO();
        staff2.setEmpInfoId("469C9C04F8DE4BDC83A0873FD11069EA");
        staff2.setEmpInfoName("员工二");
        staffList.add(staff2);

        AttendRuleStaffAddDTO staff3 = new AttendRuleStaffAddDTO();
        staff3.setEmpInfoId("5141EE3EDC554EB28638474343A982E9");
        staff3.setEmpInfoName("员工三");
        staffList.add(staff3);

        return staffList;
    }

    public List<AttendRuleWhitelistAddDTO> getWhitelist(List<EmpInfoVO> empInfoList, List<DepInfoVO> depList) {

        List<AttendRuleWhitelistAddDTO> whitelist = new ArrayList<>();

        int max = empInfoList.size() - 1,min = 1;

        for (int i = 0; i < 5; i++) {

            int ran2 = (int) (Math.random() * (max - min) + min);
            EmpInfoVO empInfoVO = empInfoList.get(ran2);

            int ran3 = (int) (Math.random()*(depList.size() - 1) + 1);
            DepInfoVO depInfoVO = depList.get(ran3);

            AttendRuleWhitelistAddDTO white1 = new AttendRuleWhitelistAddDTO();
            white1.setEmpInfoId(empInfoVO.getId());
            white1.setEmpInfoName(empInfoVO.getName());
            white1.setDepInfoId(depInfoVO.getId());
            white1.setDepInfoName(depInfoVO.getName());

            whitelist.add(white1);
        }

        return whitelist;
    }

    /**
     * 白名单
     *
     * @return
     */
    public List<AttendRuleWhitelistAddDTO> getWhitelist() {

        List<AttendRuleWhitelistAddDTO> whitelist = new ArrayList<>();

        AttendRuleWhitelistAddDTO white1 = new AttendRuleWhitelistAddDTO();
        white1.setEmpInfoId("5141EE3EDC554EB28638474343A982E9");
        white1.setEmpInfoName("员工一");
        whitelist.add(white1);

        AttendRuleWhitelistAddDTO white2 = new AttendRuleWhitelistAddDTO();
        white2.setEmpInfoId("469C9C04F8DE4BDC83A0873FD11069EA");
        white2.setEmpInfoName("员工2");
        whitelist.add(white2);

        return whitelist;
    }

    public List<AttendRuleReportAddDTO> getReportList(List<EmpInfoVO> empInfoList, List<DepInfoVO> depList) {

        List<AttendRuleReportAddDTO> reportList = new ArrayList<>();

        int max = empInfoList.size() - 1,min = 1;

        for (int i = 0; i < 5; i++) {

            int ran2 = (int) (Math.random() * (max - min) + min);
            EmpInfoVO empInfoVO = empInfoList.get(ran2);

            int ran3 = (int) (Math.random()*(depList.size() - 1) + 1);
            DepInfoVO depInfoVO = depList.get(ran3);

            AttendRuleReportAddDTO report1 = new AttendRuleReportAddDTO();
            report1.setEmpInfoId(empInfoVO.getId());
            report1.setEmpInfoName(empInfoVO.getName());
            report1.setDepInfoId(depInfoVO.getId());
            report1.setDepInfoName(depInfoVO.getName());
            reportList.add(report1);
        }

        return reportList;
    }

    /**
     * 汇报对象
     *
     * @return
     */
    public List<AttendRuleReportAddDTO> getReportList() {

        List<AttendRuleReportAddDTO> reportList = new ArrayList<>();

        AttendRuleReportAddDTO report1 = new AttendRuleReportAddDTO();
        report1.setEmpInfoId("71AB7AC5726A4416BE20016A65AC4257");
        report1.setEmpInfoName("小米");
        reportList.add(report1);

        AttendRuleReportAddDTO report2 = new AttendRuleReportAddDTO();
        report2.setEmpInfoId("726A64D987B34385BA08564A707F4E2C");
        report2.setEmpInfoName("小明");
        reportList.add(report2);

        return reportList;
    }

    /**
     * 打卡地点-位置
     *
     * @return
     */
    public List<AttendRuleLocationAddDTO> getLocationList() {

        List<AttendRuleLocationAddDTO> locationList = new ArrayList<>();

        AttendRuleLocationAddDTO local1 = new AttendRuleLocationAddDTO();
        local1.setPunchLongitude("116.644009");
        local1.setPunchLatitude("40.010055");
        local1.setPositionName("中央人民广场");
        local1.setDistance(1);
        locationList.add(local1);

        AttendRuleLocationAddDTO local2 = new AttendRuleLocationAddDTO();
        local1.setPunchLongitude("116.644009");
        local1.setPunchLatitude("40.010055");
        local2.setPositionName("中央人民广场");
        local2.setDistance(2);
        locationList.add(local2);

        AttendRuleLocationAddDTO local3 = new AttendRuleLocationAddDTO();
        local1.setPunchLongitude("116.644009");
        local1.setPunchLatitude("40.010055");
        local3.setPositionName("中央人民广场");
        local3.setDistance(3);
        locationList.add(local3);

        return locationList;
    }

    /**
     *  打卡地点-wifi
     *
     * @return
     */
    public List<AttendRuleWifiAddDTO> getWifiList() {

        List<AttendRuleWifiAddDTO> wifiList = new ArrayList<>();

        AttendRuleWifiAddDTO wifi1 = new AttendRuleWifiAddDTO();
        wifi1.setName("wifi1");
        wifi1.setMacAddr("xcxcxcxcxc");
        wifiList.add(wifi1);

        AttendRuleWifiAddDTO wifi2 = new AttendRuleWifiAddDTO();
        wifi2.setName("wifi2");
        wifi2.setMacAddr("xcxcxcxc");
        wifiList.add(wifi2);

        return wifiList;
    }


    public static void main(String[] args) {

        int max = 7,min = 1;
        int ran2 = (int) (Math.random() * (max - min) + min);

        System.out.println(ran2);

    }

    public List<AttendRulePunchTimeAddDTO> getPunchTimeListRandomFree() {

        List<AttendRulePunchTimeAddDTO> punchTimeList = new ArrayList<>();

        for (int i = 0; i < 3; i++) {
            AttendRulePunchTimeAddDTO time = new AttendRulePunchTimeAddDTO();

            List<String> weekList = new ArrayList<>();

            int max = 7,min = 1;
            int ran = (int) (Math.random() * (max - min) + min);

            for (int j = 0; j < ran; j++) {

                int ran2 = (int) (Math.random() * (max - min) + min);
                if (ran2 == 1) {
                    weekList.add("星期一");
                }
                if (ran2 == 2) {
                    weekList.add("星期二");
                }
                if (ran2 == 3) {
                    weekList.add("星期三");
                }
                if (ran2 == 4) {
                    weekList.add("星期四");
                }
                if (ran2 == 5) {
                    weekList.add("星期五");
                }
                if (ran2 == 6) {
                    weekList.add("星期六");
                }
                if (ran2 == 7) {
                    weekList.add("星期日");
                }
            }

            time.setWorkWeekList(weekList);

            punchTimeList.add(time);
        }

        return punchTimeList;
    }

    public List<AttendRulePunchTimeAddDTO> getPunchTimeListRandom() {

        List<AttendRulePunchTimeAddDTO> punchTimeList = new ArrayList<>();

        for (int i = 0; i < 3; i++) {
            AttendRulePunchTimeAddDTO time = new AttendRulePunchTimeAddDTO();

            List<String> weekList = new ArrayList<>();

            weekList.add("星期一");
            weekList.add("星期二");
            weekList.add("星期三");
            weekList.add("星期四");
            weekList.add("星期五");
            weekList.add("星期六");
            weekList.add("星期日");

            List<AttendPeriodDTO> periodList = attendPeriodTestHelper.getAttendPeriod2();

            time.setWorkWeekList(weekList);
            time.setPeriodList(periodList);

            time.setIsAllowedLateEarly(SafeKit.getInteger(getRandomValue("tinyint",0)));
            time.setLateMinute(SafeKit.getInteger(getRandomValue("int",0)));
            time.setEarlyMinute(SafeKit.getInteger(getRandomValue("int",0)));

            time.setIsAllowLateLate(SafeKit.getInteger(getRandomValue("tinyint",0)));
            time.setOffWorkLateMinute(SafeKit.getInteger(getRandomValue("int",0)));
            time.setOnWorkLateMinute(SafeKit.getInteger(getRandomValue("int",0)));

            time.setEarliestPunchTime(SafeKit.getInteger(getRandomValue("int",0)) + "");
            time.setLatestPunchTime(SafeKit.getInteger(getRandomValue("int",0)) + ""); // 最晚下班打卡时间(分钟)
            time.setAfterWorkNeedPunch(SafeKit.getInteger(getRandomValue("tinyint",0))); // 下班是否需要打卡;1-是,0-否

            punchTimeList.add(time);
        }

        return punchTimeList;
    }



    /**
     * 打卡时间
     *
     * @return
     */
    public List<AttendRulePunchTimeAddDTO> getPunchTimeList() {

        List<AttendRulePunchTimeAddDTO> punchTimeList = new ArrayList<>();

        AttendRulePunchTimeAddDTO time1 = new AttendRulePunchTimeAddDTO();
        List<String> weekList = new ArrayList<>();
        weekList.add("星期一");
        weekList.add("星期二");
        time1.setWorkWeekList(weekList); // 工作周;星期一,星期二

        List<AttendPeriodDTO> periodList = new ArrayList<>();

        AttendPeriodDTO attendPeriod1 = new AttendPeriodDTO();
        attendPeriod1.setStartPeriod("08:00");
        attendPeriod1.setEndPeriod("09:00");
        attendPeriod1.setType(1);
        periodList.add(attendPeriod1);

        AttendPeriodDTO attendPeriod2 = new AttendPeriodDTO();
        attendPeriod2.setStartPeriod("08:00");
        attendPeriod2.setEndPeriod("09:00");
        attendPeriod2.setType(2);
        periodList.add(attendPeriod2);

        time1.setPeriodList(periodList); // 打卡时段;80:00-12:00,13:00-18:00

        time1.setIsAllowedLateEarly(1); // 允许迟到早退；1-允许,0-不允许
        time1.setLateMinute(5); //上班允许迟到(分钟)
        time1.setEarlyMinute(5); // 下班允许早退(分钟)

        time1.setIsAllowLateLate(1); // 允许晚走晚到;1-允许,0-不允许
        time1.setOffWorkLateMinute(5); // 下班晚走(分钟)
        time1.setOnWorkLateMinute(5); // 上班晚到(分钟)

        time1.setEarliestPunchTime("80"); // 最早上班打卡时间
        time1.setLatestPunchTime("90"); // 最晚下班打卡时间(分钟)
        time1.setAfterWorkNeedPunch(1); // 下班是否需要打卡;1-是,0-否

        punchTimeList.add(time1);

        return punchTimeList;
    }

    public List<NeedPunchDateAddDTO> getNeedPunchDateList() {

        List<NeedPunchDateAddDTO> needPunchDateList = new ArrayList<>();

        for (int i = 0; i < 3; i++) {

            NeedPunchDateAddDTO needPunchDate1 = new NeedPunchDateAddDTO();
            needPunchDate1.setDaily(new SimpleDateFormat("yyyy-MM-dd").format(randomDate("2020-01-01","2020-02-02")));
            needPunchDate1.setPeriodList(attendPeriodTestHelper.getAttendPeriod());
            needPunchDate1.setReason("xxxx");
            needPunchDateList.add(needPunchDate1);

        }
        return needPunchDateList;
    }

    private static Date randomDate(String beginDate, String endDate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);

            if(start.getTime() >= end.getTime()){
                return null;
            }
            long date = random(start.getTime(),end.getTime());
            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long random(long begin,long end){
        long rtn = begin + (long)(Math.random() * (end - begin));
        if(rtn == begin || rtn == end){
            return random(begin,end);
        }
        return rtn;
    }

    /**
     * 特殊日期-不必打卡的日期
     *
     * @return
     */
    public List<NoNeedPunchDateAddDTO> getNoNeedPunchDateList() {

        List<NoNeedPunchDateAddDTO> noNeedPunchDateList = new ArrayList<>();

        NoNeedPunchDateAddDTO noNeedPunch1 = new NoNeedPunchDateAddDTO();
        noNeedPunch1.setDaily(new SimpleDateFormat("yyyy-MM-dd").format(randomDate("2020-01-01","2020-02-02")));
        noNeedPunch1.setReason("xxxxx");

        NoNeedPunchDateAddDTO noNeedPunch2 = new NoNeedPunchDateAddDTO();
        noNeedPunch2.setDaily(new SimpleDateFormat("yyyy-MM-dd").format(randomDate("2020-01-01","2020-02-02")));
        noNeedPunch2.setReason("xxxxx");

        noNeedPunchDateList.add(noNeedPunch1);
        noNeedPunchDateList.add(noNeedPunch2);

        return noNeedPunchDateList;
    }

    public TokenUser buildEmpInfoTokenUser(EmpInfoVO empInfoVO) {

        TokenUser tokenUser = new TokenUser();
        tokenUser.setSiteId(empInfoVO.getSiteId());
        tokenUser.setUserId(empInfoVO.getUserId());
        tokenUser.setSiteName("所属站点");
        tokenUser.setUserName(empInfoVO.getName());
        return tokenUser;
    }
}
